# 导入包
import requests

# 将url 定义成一个变量 后续调用
url = "https://mall.deyunce.com/api/account/login"

# 书写请求头 授权 文本格式申明  都是字典的键值对
head = {
    "authorization" : "Basic ZGV5dW5jZTo4MjgxMjM=",
    "content-type" : 'application/json'
}
# 添加一个body变量 收容账号密码的键值对 生产字典
body = {"account" : "17319774151","password" : "Dlh41882.","client" : 5}
# 发一个登录接口 发送的结果赋值给一个变量res或者response结果
res = requests.post(url=url,headers=head,json=body)

# 通过print打印的状态码 返回信息
print(res.status_code)
# print(res.json())
tk = res.json()["data"]["token"]
# print(tk)

head["token"] = tk
# print(head)

# 看个人中心
url2 = "https://mall.deyunce.com/api/user/info"
res2 = requests.get(url2,headers=head)

print(res2.json())
assert res2.status_code == 200
assert res2.json()["msg"] == "获取成功"

url3 = "https://mall.deyunce.com/api/pc/goodsList"
sear = input("输入要查询的商品:")

body = {"page_size":20,"name":sear}

res = requests.get(url=url3,headers=head,params=body)
gid = res.json()["data"]["list"][7]["id"]

# 商品详情结构发通 获得 id(item_id)
url4 = "https://mall.deyunce.com/api/goods/getGoodsDetail"

res = requests.get(url4,headers=head,params={"id":gid})
itemid = res.json()["data"]["goods_item"][0]["id"]

print(itemid)

# buy
url5 = "https://mall.deyunce.com/api/order/buy"
body = {"action":"submit","goods":[{"item_id":itemid,"num":1}],"delivery_type":1}

res = requests.post(url=url5,headers=head,json=body)
print(res.json())